package trastienda.servlet.categoria;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import trastienda.dao.CategoriaDAO;
import trastienda.excepcion.DAOExcepcion;
import trastienda.modelo.Categoria;

/**
 * Servlet implementation class for Servlet: CategoriaActualizarServlet
 * 
 */
public class CategoriaActualizarServlet extends javax.servlet.http.HttpServlet
		implements javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public CategoriaActualizarServlet() {
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int id = Integer.parseInt(request.getParameter("idCategoria"));
		Categoria vo = new Categoria();
		vo.setIdCategoria(id);
		vo.setNombre(request.getParameter("nombre"));
		vo.setDescripcion(request.getParameter("descripcion"));

		CategoriaDAO dao = new CategoriaDAO();
		try {
			dao.actualizar(vo);
			request.getRequestDispatcher("categoriaBuscar.jsp").forward(
					request, response);
		} catch (DAOExcepcion e) {
			System.err.println("Error");
			RequestDispatcher rd = request.getRequestDispatcher("/error.jsp");
			rd.forward(request, response);
		}

	}
}